package com.zd.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Server04 {

//    public static List<Socket> socketList = new ArrayList<>();

    public  static HashMap<String , Socket> username2socket = new HashMap<>();

    public static void main(String[] args) throws IOException {

        // 1 创建服务器 , 需要指定端口号
        ServerSocket serverSocket =
                new ServerSocket(8888);

        while (true)
        {
            // 2 启动服务器 监听客户端的连接  这样代码会线程阻塞 等，如果么有客户端的连接，一直等
            Socket socket = serverSocket.accept();

            System.out.println("服务器启动成功 , 开始监听端口 , 等待客户端的连接");

            ServerThread04 serverThread = new ServerThread04(socket);
            serverThread.start();
        }


    }
}

class ServerThread04 extends Thread{

    private Socket socket;

    public ServerThread04(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {

        PrintStream printStream = null;

        try {
            printStream = new PrintStream(socket.getOutputStream());

            // 服务端读取客户端任意时刻发送过来的数据
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String line = null;

            // 如果读取到客户端的数据 就给打印到控制台 否则一直读  线程阻塞问题
            while( (line = bufferedReader.readLine()) != null){

                System.out.println("服务器端收到的客户端发过来的数据: " + line);
//                客户端给服务器发送
//                1,zs		注册
//
//                2,zs,ls,hello  私聊
//
//                3,zs,hello everyone!
//
//                        服务器给客户端发送
//                1,ok		注册成功
//                1,nook,username is exist! 注册失败
//
//                2,zs,ls,hello  私聊
//
//
//                3,zs,hello everyone!

                String[] args = line.split(",");

                String returnMessage = null;

                // 1,zs
                if("1".equals(args[0]))
                {

                    if ( !Server04.username2socket.containsKey(args[1]) )
                    {
                        Server04.username2socket.put(args[1], socket);

                        returnMessage = "1,ok";
                    }else{
                        returnMessage = "1,nook,username is exist!";
                    }

                    printStream.println(returnMessage);
                    printStream.flush();

                }

                // 私聊 2,zs,ls,hello
                if("2".equals(args[0]))
                {
                    Socket receiveSocket = Server04.username2socket.get(args[2]);
                    PrintStream receivePrintStream = new PrintStream(receiveSocket.getOutputStream());

                    receivePrintStream.println(line);
                    receivePrintStream.flush();

                }

                // 群聊 3,zs,hello everyone!
                if("3".equals(args[0]))
                {
                    // 给所有客户端发送数据
                    for(Socket sock : Server04.username2socket.values())
                    {
                        printStream = new PrintStream(sock.getOutputStream());
                        printStream.println(line);
                        printStream.flush();
                    }

                }

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}


